import edu.udo.cs.wvtool.generic.output.WVTOutputFilter;
import edu.udo.cs.wvtool.main.WVTWordVector;
import edu.udo.cs.wvtool.util.WVToolException;
import edu.udo.cs.wvtool.wordlist.WVTWordList;
import net.sf.javaml.core.Dataset;
import net.sf.javaml.core.DefaultDataset;
import net.sf.javaml.core.DenseInstance;
import net.sf.javaml.core.Instance;
import net.sf.javaml.distance.PearsonCorrelationCoefficient;
import net.sf.javaml.featureselection.subset.GreedyBackwardElimination;
import net.sf.javaml.tools.data.FileHandler;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: tobias
 * Date: 5/5/11
 * Time: 4:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class DataSet extends DefaultDataset implements WVTOutputFilter {

    private WVTWordList wordlist;
    private ArrayList<String> classes = new ArrayList<String>();
    private HashMap<Integer, String> classIndex;
    private Set<Integer> rm_attr = new HashSet<Integer>();

    public DataSet(HashMap<Integer, String> clas) {
        super();
        this.classIndex = clas;
    }
    public DataSet(Dataset dataset) {
        for(Instance i : dataset) {
            this.add(i);
        }
    }

    public void write(WVTWordVector wv) throws WVToolException {
        write(new WVector(wv));
    }
    public void write(WVector wv) throws WVToolException {
        double[] values = wv.getValues();
		String label = classIndex.get(wv.getLabel());
        Instance instance = new DenseInstance(values, label);
		//instance.setClassValue(label);
        if(!classes.contains(label)) {
            classes.add(label);
        }
        this.add(instance);
    }

    public void setFeatureSetSize(int n) {

        GreedyBackwardElimination ga =
                new GreedyBackwardElimination(n, new PearsonCorrelationCoefficient());

        ga.build(this);
        int max = wordlist.getNumWords();
        ArrayList words = new ArrayList();
        for(int i = 0; i < max; i++) {
            if(!ga.selectedAttributes().contains(i)) {
				rm_attr.add(i);

                words.add(wordlist.getWord(i));
            }
        }
        for(Instance inst : this) {
			inst.removeAttributes(rm_attr);
		}
        wordlist.removeWord(rm_attr);

    }
    public void store(File file, File wordfile) throws IOException {

        wordlist.store(new FileWriter(wordfile));
        FileHandler.exportDataset(this, file);
    }
    public void addWordList(WVTWordList wordlist) {
        this.wordlist = wordlist;
    }
    public WVTWordList getWordList() {
        return wordlist;
    }

}
